# -*- coding: utf-8 -*-
# @Author: Tim Liu
# @Date: 2024-04-15
# @Last Modified by: Tim Liu
# @Last Modified time: 2024-04-15

# @Description: api endpoints for aigc account api key


from typing import Optional

from fastapi import APIRouter, Depends
from sqlalchemy.orm import joinedload

from crewplus.apps.apikey.crud import AccountApiKeyDal
from crewplus.apps.apikey.schemas.api_key import ApiKeyDel
from crewplus.apps.account.account import AccountDal
from apps.vadmin.auth.models import VadminUser
from apps.vadmin.auth.params import UserParams
from apps.vadmin.auth.schemas import UserOut
from utils.response import SuccessResponse

from apps.vadmin.auth.utils.current import FullAdminAuth, AllUserAuth
from apps.vadmin.auth.utils.validation.auth import Auth


app = APIRouter()


###########################################################
# api key管理
###########################################################
# @app.get("/get_key", summary="123123")
# async def get_users(
#         params: UserParams = Depends(),
#         auth: Auth = Depends(FullAdminAuth(permissions=["auth.user.list"]))
# ):
#     model = VadminUser
#     options = [joinedload(model.roles)]
#     schema = UserOut
#     datas, count = await UserDal(auth.db).get_datas(
#         **params.dict(),
#         v_options=options,
#         v_schema=schema,
#         v_return_count=True
#     )
#     return SuccessResponse(datas, count=count)


@app.post("/create_api_key", summary="account creates new api key")
async def create_api_key(api_key_name: str, auth: Auth = Depends(AllUserAuth())):
    return SuccessResponse(await AccountApiKeyDal(auth.db).create_api_key_data(user_id=auth.user.id, api_key_name=api_key_name))

@app.get("/get_api_key", summary="get all api keys of user or certain api key name")
async def get_api_key(api_key_name: Optional[str] = None, page: Optional[int] = None, limit: Optional[int] = None, auth: Auth = Depends(AllUserAuth())):
    return SuccessResponse(await AccountApiKeyDal(auth.db).get_api_key_data(user_id=auth.user.id, api_key_name=api_key_name, page=page, limit=limit))

@app.delete("/delete_api_key", summary="remove api key by name")
async def delete_api_key(message: ApiKeyDel, auth: Auth = Depends(AllUserAuth())):
    return SuccessResponse(await AccountApiKeyDal(auth.db).delete_api_key_data(user_id=message.user_id, api_key_name=message.api_key_name))

